package com.neusoft.hifly.security;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Title: Kernel Library
 * <p>
 * Description: DataEncryption/解密
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
public final class EncodeUtils {
	private static final Logger Log = LoggerFactory.getLogger(EncodeUtils.class);

	private static final String[] HEX = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e",
			"f" };

	/**
	 * 通过共享码加密字符串
	 *
	 * @param input
	 *            字符串
	 * @param shareCode
	 *            共享码
	 * @return 加密
	 */
	public static String encode(final String input, final String shareCode) {
		if (StringUtils.isNotEmpty(input)) {
			return EncodeUtils.md5Encode(input + shareCode);
		} else {
			return "";
		}
	}

	/**
	 * MD5加密字符串
	 *
	 * @param origin
	 *            字符串
	 * @return 加密
	 */
	public static String md5Encode(final String origin) {
		String resultString = origin;
		try {
			final MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = EncodeUtils.byteArrayToHexString(md.digest(resultString.getBytes()));
		} catch (final Exception e) {
			Log.error(e.getMessage());
		}
		return resultString;
	}

	/**
	 * MD5加密字节数组
	 *
	 * @param input
	 *            字节数组
	 * @return 加密
	 */
	public static String md5Encode(final byte[] input) {
		String resultString = null;
		try {
			final MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = EncodeUtils.byteArrayToHexString(md.digest(input));
		} catch (final Exception e) {
			Log.error(e.getMessage());
		}
		return resultString;
	}

	/**
	 * Base64 加密
	 *
	 * @param input
	 *            需要加密的字符串
	 * @param encode
	 *            字符编码
	 * @return 加密后的字符串
	 */
	public static String encodeBase64Str(final String input, final String encode) {
		if ((input != null) && !"".equals(input)) {
			String result = "";
			if (StringUtils.isNotEmpty(encode)) {
				// TODO 解决中文乱码问题，根据解密端的字符集编码设置 2010.06.21 Charles
				try {
					result = new String(Base64.encodeBase64(input.getBytes(encode)));
				} catch (final UnsupportedEncodingException e) {
					Log.error(e.getMessage());
					result = "";
				}
			} else {
				result = new String(Base64.encodeBase64(input.getBytes()));
			}
			return result;
		} else {
			return "";
		}
	}

	/**
	 * Base64 解密
	 *
	 * @param input
	 *            需要解密的字符串
	 * @param encode
	 *            字符编码
	 * @return 解密后的字符串
	 */
	public static String decodeBase64Str(final String input, final String encode) {
		String result = "";
		if (StringUtils.isNotEmpty(encode)) {
			// TODO 解决中文乱码问题，根据解密端的字符集编码设置 2010.06.21 Charles
			try {
				result = new String(Base64.decodeBase64(input.getBytes(encode)));
			} catch (final UnsupportedEncodingException e) {
				Log.error(e.getMessage());
				result = "";
			}
		} else {
			result = new String(Base64.decodeBase64(input.getBytes()));
		}
		return result;
	}

	/**
	 * 转换字节数组为16进制字串
	 *
	 * @param b
	 *            字节数组
	 * @return 16进制字串
	 */

	private static String byteArrayToHexString(final byte[] b) {
		final StringBuffer resultSb = new StringBuffer();
		for (final byte element : b) {
			resultSb.append(EncodeUtils.byteToHexString(element));
		}
		return resultSb.toString();
	}

	/**
	 * 转换字节为16进制字串
	 *
	 * @param b
	 *            字节
	 * @return 16进制字串
	 */
	private static String byteToHexString(final byte b) {
		// int n = b;
		// if (n < 0)
		// n = 256 + n;
		// int d1 = n / 16;
		// int d2 = n % 16;
		return EncodeUtils.HEX[b >>> 4 & 0xf] + EncodeUtils.HEX[b & 0xf];
		// return hexDigits[d1] + hexDigits[d2];
	}

	/**
	 * 演示
	 *
	 * @param args
	 *            参数
	 */
	public static void main(final String[] args) {
		try {
			System.out.println("1234563232232323232323".getBytes("232323aa"));
		} catch (final UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 通过共享码加密字符串
		final String str = "j8&&??>><<()****abck中文";
		final String shareCode = "abcd";
		//		1ea8fb03e49c8e90064df042631824df
		System.out.println(EncodeUtils.encode(str, shareCode));

		//MD5加密字符串
		//		81dc9bdb52d04dc20036dbd8313ed055
		System.out.println(EncodeUtils.md5Encode("1234"));
	}
}
